﻿using System;
using System.Threading;
using TYBase;
using TYRTUBase.Comm;

namespace TYRTUBase
{


    public class Device_IR_3562_SysRtu : Device_IR
    {

        public Device_IR_3562_SysRtu(string equipId, string equipName) : base(equipId, equipName, true)
        {
            ReadOutTime = 2000;
            //SetNewLine(new byte[] { 0x0D, 0x0A });
            var para = new ScanerPara(DefaultPara);
            para.NewLine = new byte[] { 0x0d, 0x0a };
            SetPara(para);
        }


        public override bool InitIR(EnumIRMeasSpd irSpd)
        {
            if (!base.InitIR(irSpd))
                return false;

            #region 消息头去掉 ***不然的话回来的会是 :SYSTEM:HEADER OFF****

            {
                //1:发送消息头信号
                string sendCmd = ":SYST:HEAD OFF";
                rtu.Write(ToCmdByte(sendCmd));
                Thread.Sleep(10);
                //2.查询结果  查询控制头的   <ON/ OFF>
                sendCmd = ":SYST:HEAD?";
                rtu.Write(ToCmdByte(sendCmd));

                DateTime dt = DateTime.Now;
                byte[] dataRead = null;
                while (true)
                {
                    if (IsRun == false)
                        return false;
                    if (DateTime.Now - dt > TimeSpan.FromMilliseconds(ReadOutTime))
                    {
                        Log.E("ir rec out time");
                        return false;
                    }
                    Thread.Sleep(10);
                    if (rtu.Read(out byte[] dataTmp) > 0)
                    {
                        dataRead = dataRead.Append(dataTmp);
                        if (dataRead != null && dataRead.Length >= 5)
                        {
                            if (dataRead.AsString().IndexOf("OFF") != -1)
                            {
                                break;
                            }
                            else
                            {
                                return false;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
            }

            #endregion 消息头去掉 ***不然的话回来的会是 :SYSTEM:HEADER OFF****

            #region 设置读取速率

            {
                //1:发送消息头信号
                string sendCmd = $":SAMP:RATE {irSpd}";
                rtu.Write(ToCmdByte(sendCmd));
                Thread.Sleep(10);
                //2.查询结果  查询读取速度的    <EXFAST/ FAST/ MEDIUM/ SLOW>
                sendCmd = ":SAMP:RATE?";
                rtu.Write(ToCmdByte(sendCmd));

                DateTime dt = DateTime.Now;
                byte[] dataRead = null;
                while (true)
                {
                    if (IsRun == false)
                        return false;
                    if (DateTime.Now - dt > TimeSpan.FromMilliseconds(ReadOutTime))
                    {
                        Log.E("内阻仪反馈:设置速度超时");
                        return false;
                    }
                    Thread.Sleep(10);
                    if (rtu.Read(out byte[] dataTmp) > 0)
                    {
                        dataRead = dataRead.Append(dataTmp);
                        if (dataRead != null)
                        {
                            if (dataRead.AsString().IndexOf($"{irSpd}") != -1)
                            {
                                break;
                            }
                            else
                            {
                                Log.E("内阻仪反馈:设置速度反馈不正确");
                                return false;
                            }
                        }
                    }
                }
            }

            #endregion 设置读取速率

            #region 设置电压内阻读取格式-RV电压内阻都读取模式

            {
                //1:发送消息头信号
                string sendCmd = ":FUNC RV";
                rtu.Write(ToCmdByte(sendCmd));
                Thread.Sleep(10);
                //2.查询结果  查询读取速度的    <RV/ RESISTANCE/ VOLTAGE>
                sendCmd = ":FUNC?";
                rtu.Write(ToCmdByte(sendCmd));

                DateTime dt = DateTime.Now;
                byte[] dataRead = null;
                while (true)
                {
                    if (IsRun == false)
                        return false;
                    if (DateTime.Now - dt > TimeSpan.FromMilliseconds(ReadOutTime))
                    {
                        Log.E("内阻仪反馈:电压内阻读取格式超时");
                        return false;
                    }
                    Thread.Sleep(10);
                    if (rtu.Read(out byte[] dataTmp) > 0)
                    {
                        dataRead = dataRead.Append(dataTmp);
                        if (dataRead != null && dataRead.Length >= 4)
                        {
                            if (dataRead.AsString().IndexOf("RV") != -1)
                            {
                                break;
                            }
                            else
                            {
                                Log.E("内阻仪反馈:电压内阻读取格式反馈不正确");
                                return false;
                            }
                        }
                    }
                }
            }

            #endregion 设置电压内阻读取格式-RV电压内阻都读取模式

            #region 控制触发式测量

            {

                string sendCmd = ":INITiate:CONTinuous OFF";//
                rtu.Write(ToCmdByte(sendCmd));
                Thread.Sleep(10);

                //1:发送消息头信号
                sendCmd = ":TRIG:SOUR IMM";//
                rtu.Write(ToCmdByte(sendCmd));
                Thread.Sleep(10);
                //2.查询结果  触发式测量    <IMMEDIATE/ EXTERNAL>
                sendCmd = ":TRIG:SOUR?";
                rtu.Write(ToCmdByte(sendCmd));

                DateTime dt = DateTime.Now;
                byte[] dataRead = null;
                while (true)
                {
                    if (IsRun == false)
                        return false;
                    if (DateTime.Now - dt > TimeSpan.FromMilliseconds(ReadOutTime))
                    {
                        Log.E("内阻仪反馈:触发式测量反馈超时");
                        return false;
                    }
                    Thread.Sleep(10);
                    if (rtu.Read(out byte[] dataTmp) > 0)
                    {
                        dataRead = dataRead.Append(dataTmp);
                        if (dataRead != null && dataRead.Length >= 11)
                        {
                            if (dataRead.AsString().IndexOf("IMMEDIATE") != -1)
                            {
                                break;
                            }
                            else
                            {
                                Log.E("内阻仪反馈:触发式测量反馈不正确");
                                return false;
                            }
                        }
                    }
                }
            }

            #endregion 控制触发式测量


            return true;
        }

        public bool SetSpeed(EnumIRMeasSpd IRSpeed)
        {
            #region 设置读取速率

            Spd = IRSpeed;
            Log.I("开始初始化IR串口");
            if (IsXModerMode)
            {
                return true;
            }
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                return false;
            }
            {
                //1:发送消息头信号
                string sendCmd = $":SAMP:RATE {IRSpeed}";
                rtu.Write(ToCmdByte(sendCmd));
                Thread.Sleep(10);
                //2.查询结果  查询读取速度的    <EXFAST/ FAST/ MEDIUM/ SLOW>
                sendCmd = ":SAMP:RATE?";
                rtu.Write(ToCmdByte(sendCmd));

                DateTime dt = DateTime.Now;
                byte[] dataRead = null;
                while (true)
                {
                    if (IsRun == false)
                        return false;
                    if (DateTime.Now - dt > TimeSpan.FromMilliseconds(ReadOutTime))
                    {
                        Log.E("内阻仪反馈:设置速度超时");
                        return false;
                    }
                    Thread.Sleep(10);
                    if (rtu.Read(out byte[] dataTmp) > 0)
                    {
                        dataRead = dataRead.Append(dataTmp);
                        if (dataRead != null)
                        {
                            if (dataRead.AsString().IndexOf($"{IRSpeed}") != -1)
                            {
                                return true;
                            }
                            else
                            {
                                Log.E("内阻仪反馈:设置速度反馈不正确");
                                return false;
                            }
                        }
                    }
                }
                return false;
            }

            #endregion 设置读取速率
        }

        public bool SendInit_1()
        {
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                return false;
            }

            #region 消息头去掉 ***不然的话回来的会是 :SYSTEM:HEADER OFF****

            //1:发送消息头信号
            string sendCmd = ":SYST:HEAD OFF";
            rtu.Write(ToCmdByte(sendCmd));
            Thread.Sleep(10);
            //2.查询结果  查询控制头的   <ON/ OFF>
            sendCmd = ":SYST:HEAD?";
            rtu.Write(ToCmdByte(sendCmd));

            #endregion 消息头去掉 ***不然的话回来的会是 :SYSTEM:HEADER OFF****

            return true;
        }

        public bool SendInit_2()
        {
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                return false;
            }

            #region 设置读取速率

            {
                //1:发送消息头信号
                string sendCmd = $":SAMP:RATE {EnumIRMeasSpd.MEDIUM}";
                rtu.Write(ToCmdByte(sendCmd));
                Thread.Sleep(10);
                //2.查询结果  查询读取速度的    <EXFAST/ FAST/ MEDIUM/ SLOW>
                sendCmd = ":SAMP:RATE?";
                rtu.Write(ToCmdByte(sendCmd));
            }

            #endregion 设置读取速率

            return true;
        }

        public bool SendInit_3()
        {
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                return false;
            }

            #region 设置电压内阻读取格式-RV电压内阻都读取模式

            {
                //1:发送消息头信号
                string sendCmd = ":FUNC RV";
                rtu.Write(ToCmdByte(sendCmd));
                Thread.Sleep(10);
                //2.查询结果  查询读取速度的    <RV/ RESISTANCE/ VOLTAGE>
                sendCmd = ":FUNC?";
                rtu.Write(ToCmdByte(sendCmd));
            }

            #endregion 设置电压内阻读取格式-RV电压内阻都读取模式

            return true;
        }

        public bool SendInit_4()
        {
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                return false;
            }

            #region 设置内阻量程

            {
                //1:发送消息头信号
                string sendCmd = ":RES:RANG 300.00E-3";//  < 30.000E-3   3.0000E-3  300.00E-3"-3561 只有这个量程;
                rtu.Write(ToCmdByte(sendCmd));
                Thread.Sleep(10);
                //2.查询结果  内阻量程
                sendCmd = ":RES:RANG?";
                rtu.Write(ToCmdByte(sendCmd));
            }

            #endregion 设置内阻量程

            return true;
        }

        public bool SendInit_5()
        {
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                return false;
            }

            #region 设置电压量程

            {
                //1:发送消息头信号
                string sendCmd = ":VOLT:RANG 6";//
                rtu.Write(ToCmdByte(sendCmd));
                Thread.Sleep(10);
                //2.查询结果  查询读取速度的    <5.0000E+0(NR3)
                sendCmd = ":VOLT:RANG?";
                rtu.Write(ToCmdByte(sendCmd));
            }

            #endregion 设置电压量程

            return true;
        }

        public bool SendInit_6()
        {
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                return false;
            }

            #region 控制触发式测量

            {
                //1:发送消息头信号
                string sendCmd = ":TRIG:SOUR IMM";//
                rtu.Write(ToCmdByte(sendCmd));
                Thread.Sleep(10);
                //2.查询结果  触发式测量    <IMMEDIATE/ EXTERNAL>
                sendCmd = ":TRIG:SOUR?";
                rtu.Write(ToCmdByte(sendCmd));
            }

            #endregion 控制触发式测量

            return true;
        }

        public bool SendInit_7()
        {
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                return false;
            }

            #region 设定为连续测量 OFF

            {
                //1:发送消息头信号
                string sendCmd = ":INIT:CONT OFF";//
                rtu.Write(ToCmdByte(sendCmd));
                Thread.Sleep(10);
                //2.查询结果  触发式测量    <IMMEDIATE/ EXTERNAL>
                sendCmd = ":INIT:CONT?";
                rtu.Write(ToCmdByte(sendCmd));
            }

            #endregion 设定为连续测量 OFF

            return true;
        }

        public bool SendRead()
        {
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                return false;
            }
            Log.I("内阻仪:开始读取数据");
            string sendCmd = ":READ?";
            rtu.Write(ToCmdByte(sendCmd));
            return true;
        }



        public override bool ReadData(out string data)
        {
            if (IsXModerMode)
            {
                double ocv = _rand.Next((int)(XMode_IR_Para.OCVMin * 10000), (int)(XMode_IR_Para.OCVMax * 10000)) * 1.0d / 10000;
                double ir = _rand.Next((int)(XMode_IR_Para.IRMin * 10000), (int)(XMode_IR_Para.IRMax * 10000)) * 1.0d / 10000;
                data = IRDataHelper.OCVIR_2_String(ocv, ir);
                return true;
            }
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                data = "";
                return false;
            }
            string sendCmd = ":READ?";
            rtu.ClearInBuff();
            rtu.Write(ToCmdByte(sendCmd));

            DateTime dt = DateTime.Now;
            byte[] dataRead = null;
            while (true)
            {
                if (IsRun == false)
                {
                    data = "";
                    return false;
                }
                if (DateTime.Now - dt > TimeSpan.FromMilliseconds(ReadOutTime))
                {
                    Log.E($"ir:read outtime:{dataRead.AsString()} newLine {NewLine.AsHexString()} outTime:{ReadOutTime}");
                    data = "";
                    return false;
                }
                Thread.Sleep(10);
                if (rtu.Read(out byte[] dataTmp) > 0)
                {
                    dataRead = dataRead.Append(dataTmp);
                    if (dataRead != null && dataRead.Length > 0)
                    {
                        var listbytes = dataRead.Split(NewLine);
                        if (listbytes != null && listbytes.Count > 0)
                        {
                            var lastData = listbytes[listbytes.Count - 1];
                            var index = lastData.Indexof(NewLine);
                            if (index > 0)
                            {
                                byte[] dataget = new byte[index];
                                Array.Copy(lastData, dataget, index);
                                data = dataget.AsString();
                                return true;
                            }
                        }
                    }
                }
                else { continue; }
            }
        }
    }
}